﻿using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;
using ECatalog.Models;
using ECatalog.Models.Mailer;
using ECatalog.Models.ModelSizes;
using ECatalog.Models.UserModel;

namespace ECatalog.Controllers
{
    [Authorize]
    public class SizeModelController : Controller
    {
        private readonly IUserRepository userRepository;
        private readonly IModelSizeRepository modelSizeRepository;
        private readonly IMailer mailer;

        public SizeModelController(IUserRepository userRepository, IModelSizeRepository modelSizeRepository, IMailer mailer)
        {
            this.userRepository = userRepository;
            this.modelSizeRepository = modelSizeRepository;
            this.mailer = mailer;
        }

        [Authorize]
        [HttpPost]
        public PartialViewResult Add(ModelSize modelSize, string value)
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            if (Request.IsAjaxRequest())
            {
                modelSize = modelSizeRepository.Add(modelSize, value);
                mailer.Notify(modelSize);
                return PartialView("ModelSizeItem", modelSize);
            }
            return PartialView();
        }

        [Authorize]
        public JsonResult Remove(int modelsizeid)
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            if (Request.IsAjaxRequest())
            {
                modelSizeRepository.Remove(modelsizeid);
                return Json("ok");
            }
            return Json("error");
        }
    }
}